package cn.veliger.springai.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.veliger.springai.entity.EasyDocParse;
import cn.veliger.springai.entity.EasyDocParseResult;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author veliger
 * create on 2025-07-01
 **/
public class EasyDocService {

    public static final String API_URL = "https://api.easydoc.sh/api/v1/parse";
    public static final String API_KEY = "<KEY>";

    /**
     * 提交解析
     *
     * @param file
     * @return
     */
    public EasyDocParse parse(File file) {
        // 构建表单数据
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("file", file);
        paramMap.put("mode", "lite");

        // 发送POST请求
        String result = HttpUtil.createPost(API_URL)
                .header("api-key", API_KEY)
                .form(paramMap)
                .execute()
                .body();
        return JSONUtil.toBean(result, EasyDocParse.class);
    }

    /**
     * 查询解析结果
     *
     * @param taskId
     * @return
     */
    public EasyDocParseResult parseResult(String taskId) {
        String result = HttpUtil.createGet(API_URL + "/" + taskId + "/result")
                .header("api-key", API_KEY)
                .execute()
                .body();
        return JSONUtil.toBean(result, EasyDocParseResult.class);


    }
}
